<?php

namespace common\components\aliyun;

use Yii;
use yii\base\InvalidConfigException;
use yii\helpers\Json;
use yii\helpers\ArrayHelper;
use STS\Core\Profile\DefaultProfile;
use STS\Core\DefaultAcsClient;
use STS\AssumeRoleRequest;
use STS\Core\Regions\ProductDomain;
use STS\Core\Regions\Endpoint;
use STS\Core\Regions\EndpointProvider;

/**
 * AliyunSTS
 *
 * @author emhome
 */
class AliyunSTS extends \yii\base\Component {

    public $accessKeyId = null;
    public $accessKeySecret = null;
    public $endpoint = null;
    public $bucket = null;
    public $host = null;
    //
    private $_client = null;

    /**
     * @inheritdoc
     * @throws InvalidConfigException
     */
    public function init() {
        if ($this->accessKeyId === null) {
            throw new InvalidConfigException('The "accessKeyId" property must be set.');
        } elseif ($this->accessKeySecret === null) {
            throw new InvalidConfigException('The "accessKeySecret" property must be set.');
        }

        $regionIds = array("cn-hangzhou", "cn-beijing", "cn-qingdao", "cn-hongkong", "cn-shanghai", "us-west-1", "cn-shenzhen", "ap-southeast-1");
        $productDomains = array(
            new ProductDomain("Ecs", "ecs.aliyuncs.com"),
            new ProductDomain("Rds", "rds.aliyuncs.com"),
            new ProductDomain("BatchCompute", "batchCompute.aliyuncs.com"),
            new ProductDomain("Bss", "bss.aliyuncs.com"),
            new ProductDomain("Oms", "oms.aliyuncs.com"),
            new ProductDomain("Slb", "slb.aliyuncs.com"),
            new ProductDomain("Oss", "oss-cn-hangzhou.aliyuncs.com"),
            new ProductDomain("OssAdmin", "oss-admin.aliyuncs.com"),
            new ProductDomain("Sts", "sts.aliyuncs.com"),
            new ProductDomain("Yundun", "yundun-cn-hangzhou.aliyuncs.com"),
            new ProductDomain("Risk", "risk-cn-hangzhou.aliyuncs.com"),
            new ProductDomain("Drds", "drds.aliyuncs.com"),
            new ProductDomain("M-kvstore", "m-kvstore.aliyuncs.com"),
            new ProductDomain("Ram", "ram.aliyuncs.com"),
            new ProductDomain("Cms", "metrics.aliyuncs.com"),
            new ProductDomain("Crm", "crm-cn-hangzhou.aliyuncs.com"),
            new ProductDomain("Ocs", "pop-ocs.aliyuncs.com"),
            new ProductDomain("Ots", "ots-pop.aliyuncs.com"),
            new ProductDomain("Dqs", "dqs.aliyuncs.com"),
            new ProductDomain("Location", "location.aliyuncs.com"),
            new ProductDomain("Ubsms", "ubsms.aliyuncs.com"),
            new ProductDomain("Ubsms-inner", "ubsms-inner.aliyuncs.com")
        );
        $endpoint = new Endpoint("cn-beijing", $regionIds, $productDomains);
        $endpoints = array($endpoint);
        EndpointProvider::setEndpoints($endpoints);

        // 你需要操作的资源所在的region
        // STS服务目前只有杭州节点可以签发Token
        // 签发出的Token在所有Region都可用
        // 只允许子用户使用角色
        $iClientProfile = DefaultProfile::getProfile("cn-beijing", $this->accessKeyId, $this->accessKeySecret);
        $this->_client = new DefaultAcsClient($iClientProfile);
    }

    public function getClient() {
        return $this->_client;
    }

    public function authent() {
        $roleArn = "acs:ram::1644823653065188:role/youmbe";
        $request = new AssumeRoleRequest();
        $request->setRoleSessionName("client_name");
        $request->setRoleArn($roleArn);
        $request->setDurationSeconds(3600);
        $response = $this->client->doAction($request);
        $data = Json::decode($response);
        return ArrayHelper::getValue($data, 'Credentials');
    }

}
